<?php
declare (strict_types=1);

// +----------------------------------------------------------------------
// | SCApi
// +----------------------------------------------------------------------
// | 版权所有 2021~2022 贾二小 [ http://www.ipsoul.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://www.ipsoul.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免费声明 ( http://doc.ipsoul.com/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/jiaerxiao/scapi
// | github 代码仓库：https://github.com/jiaerxiao/scapi
// +----------------------------------------------------------------------

namespace app\system\controller;

use app\common\controller\SystemBaseController;
use app\system\facade\ConfigFacade;
use hg\apidoc\annotation\Group;
use hg\apidoc\annotation\Sort;
use hg\apidoc\annotation\Desc;
use hg\apidoc\annotation\Param;
use hg\apidoc\annotation\Method;
use hg\apidoc\annotation\Returned;
use hg\apidoc\annotation\Author;
use think\App;

/**
 * 系统设置
 * @Group("System")
 * @Sort(4)
 */
class Config extends SystemBaseController
{
    /**
     * 初始化
     */
    public function initialize()
    {
        $this->logName = "系统设置";
        parent::initialize();
    }

    /**
     * 详情
     * @Returned("sysData", type="object", desc="用户信息",
     *     @Returned("name", type="string", desc="系统名称"),
     *     @Returned("logoUrl", type="string", desc="LogoUrl"),
     *     @Returned("login", type="boolean", desc="登录开关"),
     *     @Returned("passwordRules", type="string", desc="密码验证规则"),
     *     @Returned("copyright", type="string", desc="版权信息"),
     * )
     * @Returned("smsData", type="object", desc="用户信息",
     *     @Returned("open", type="boolean", desc="短信开关"),
     *     @Returned("appKey", type="string", desc="appKey"),
     *     @Returned("secretKey", type="string", desc="secretKey"),
     * )
     * @Returned("settingList", type="array", desc="用户信息",
     *     @Returned("id", type="int", desc="唯一ID"),
     *     @Returned("key", type="string", desc="KEY"),
     *     @Returned("value", type="string", desc="VALUE"),
     *     @Returned("category", type="string", desc="CATEGORY"),
     *     @Returned("title", type="string", desc="TITLE"), ref="listReturned",
     * )
     * @Author("贾二小")
     * @since 2021/11/24
     */
    public function info()
    {
        //获取数据
        $data = ConfigFacade::infoData();
        //返回
        self::success($data);
    }

    /**
     * 系统设置保存
     * @Method("PATCH")
     * @Param("name", type="string",require=true, desc="系统名称")
     * @Param("logoUrl", type="string",require=true, desc="LogoUrl")
     * @Param("login", type="boolean",require=true, desc="登录开关")
     * @Param("passwordRules", type="string",require=true, desc="密码验证规则")
     * @Param("copyright", type="string",require=true, desc="版权信息")
     * @Author("贾二小")
     * @since 2021/11/24
     */
    public function sysSaving()
    {
        //获取参数
        $paramData = $this->request->post(['name', 'logoUrl', 'login', 'passwordRules', 'copyright']);
        //参数验证
        $this->validateData($paramData);
        //保存数据
        ConfigFacade::savingData($paramData);
        //返回
        self::success();
    }

    /**
     * 短信配置保存
     * @Method("PATCH")
     * @Param("open", type="boolean",require=true, desc="短信开关")
     * @Param("appKey", type="string", desc="appKey")
     * @Param("secretKey", type="boolean", desc="secretKey")
     * @Author("贾二小")
     * @since 2021/11/24
     */
    public function smsSaving()
    {
        //获取参数
        $paramData = $this->request->post(['open', 'appKey', 'secretKey']);
        //参数验证
        $this->validateData($paramData);
        //保存数据
        ConfigFacade::savingData($paramData);
        //返回
        self::success();
    }

    /**
     * 扩展配置保存
     * @Method("PUT")
     * @Param("id", type="int", desc="唯一ID")
     * @Param("key", type="string",require=true, desc="KEY")
     * @Param("value", type="string",require=true, desc="VALUE")
     * @Param("category", type="string",require=true, desc="CATEGORY")
     * @Param("title", type="string",require=true, desc="TITLE")
     * @Author("贾二小")
     * @since 2021/11/24
     */
    public function settingSaving()
    {
        //获取参数
        $paramData = $this->request->put(['id', 'key', 'value', 'category', 'title']);
        //参数验证
        $this->validateData($paramData);
        //保存数据
        ConfigFacade::savingSettingData($paramData);
        //返回
        self::success();
    }

    /**
     * 删除
     * @Desc("扩展配置数据删除")
     * @Method("DELETE")
     * @Param("id", type="int",require=true, desc="唯一ID")
     * @Author("贾二小")
     * @since 2021/11/28
     */
    public function settingDelete()
    {
        //获取参数
        $paramData = $this->request->delete(['id']);
        //参数验证
        $this->validateData($paramData);
        //删除数据
        ConfigFacade::deleteById($paramData['id']);
        //返回
        self::success();
    }
}